package cn.tedu.logistics.controller;

import java.util.List;
import java.util.UUID;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.tedu.logistics.service.IndexService;
import cn.tedu.logistics.service.PurviewService;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.logistics.bean.Manager;
import cn.tedu.logistics.bean.Purview;
import cn.tedu.logistics.bean.indexInfo;

import cn.tedu.logistics.service.ManagerService;

@Controller
public class indexController {
	@Autowired
	private ManagerService managerService;
	@Autowired
	private PurviewService purviewService;
	@Autowired
	private IndexService indexService;
	//显示管理员登录页面
	@RequestMapping("/showIndex.do")
	public String showIndex() {
		return  "index";
	}

	//处理登录请求
	@RequestMapping("/login.do")
	public String login(String admin, String PWD, HttpSession session, ModelMap map, HttpServletResponse response) {
		Manager manager=managerService.selectByManager(admin);
		String md5password =
				DigestUtils.md5DigestAsHex(PWD.getBytes());
		//暂时取消加密
		if(manager!=null && manager.getPWD().equals(md5password)) {

			session.setAttribute("manager", manager);
			//查询权限
			Purview purview = purviewService.PurviewById(manager.getId());
			session.setAttribute("pur", purview);
			String uuid = UUID.randomUUID().toString();
			//登录成功
			Cookie cookie = new Cookie("UserCookie",uuid);
			cookie.setPath("/");
			cookie.setMaxAge(24*60*60);
			response.addCookie(cookie);
			return "redirect:firstPage.do";
		}else {
			//登录失败
			map.addAttribute("login_failed", "用户名或者密码不正确");
			return "index";
		}

	}

	@RequestMapping("/firstPage.do")
	public String firstPage(Model model){
//		List<indexInfo> info=indexService.findObjects();
//		model.addAttribute("info", info);
		return "indexFirstPage";
	}
	
    	//点击更改口令页面
		@RequestMapping("/showManagerInfo.do")
		public String updateManager() {
			return "showManagerInfo";
		}
		//更改管理员密码成功
		@RequestMapping("/updateManager.do")
		public String updataSuccess(String admin,String oldpwd,String newpwd,Model model) {
			Manager manager = managerService.selectByManager(admin);
			if(manager!=null && manager.getPWD().equals(oldpwd)) {
				if(newpwd=="") {
					model.addAttribute("success","你输入的新密码不能为空");
					return "showManagerInfo";
				}
				managerService.updataByAdmin(admin, newpwd);
				model.addAttribute("success","修改密码成功");
				return "showManagerInfo";
			}else {
				if(oldpwd=="") {
					model.addAttribute("error","你输入的旧密码不能为空");
					if(newpwd=="") {
						model.addAttribute("success","你输入的新密码不能为空");
					}
					return "showManagerInfo";
				}

				model.addAttribute("error", "你输入的旧密码不正确");
				if(newpwd=="") {
					model.addAttribute("success","你输入的新密码不能为空");
				}
				return "showManagerInfo";
			}
			
		}
		
	//退出请求
	@RequestMapping("/exit.do")
	public String exit(HttpSession session,HttpServletResponse response) {
		//session立即无条件失效
	  session.invalidate();
	  Cookie cookie = new Cookie("UserCookie","");
		cookie.setPath("/");
		cookie.setMaxAge(24*60*60);
		response.addCookie(cookie);
		return "index";
		
	}
	
}
